package com.yanqu.road.server.manager.douluodalu;

import com.yanqu.road.entity.activity.ActivityConditionInfo;
import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.entity.enums.eActivityType;
import com.yanqu.road.logic.bussiness.servercenter.ActivityBussiness;
import com.yanqu.road.server.CrossBaseServer;
import com.yanqu.road.server.TempMgr;
import com.yanqu.road.server.manager.activity.ActivityMgr;
import com.yanqu.road.utils.date.DateHelper;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class CrossDouLuoDaLuActivityMgr extends TempMgr {

    @Override
    public boolean init() throws Exception {
        return reload();
    }

    @Override
    public boolean reloadConfig() throws Exception {
        String nowTimeStr = DateHelper.getCurrentDateTimeString();
        List<Integer> activityTypeList = new ArrayList<>();
        activityTypeList.add(eActivityType.DouLuoDaLuActivity.getValue());
        Map<Integer, ActivityInfo> activityInfoMap = ActivityBussiness.getOpenCrossRankActivityInfoMapByCrossServerId(activityTypeList, nowTimeStr, CrossBaseServer.getInstance().getServerId());
        List<Integer> activityIdList = new ArrayList<>(activityInfoMap.keySet());
        Map<Integer, List<ActivityConditionInfo>> activityConditionInfoMap = ActivityBussiness.getOpenCrossRankActivityConditionInfoMap(activityIdList);
        for (ActivityInfo activityInfo : activityInfoMap.values()) {
            initActivityConfig(activityInfo, activityConditionInfoMap.get(activityInfo.getActivityId()));
        }
        return true;
    }

    private static void initActivityConfig(ActivityInfo activityInfo, List<ActivityConditionInfo> activityConditionInfoList) {
        ActivityMgr.putActivity(activityInfo, activityConditionInfoList);
    }

    @Override
    public boolean reloadData() throws Exception {
        return true;
    }

    @Override
    public boolean stop() {
        return true;
    }


    public static ActivityInfo getActivityInfo(int activityId){
        ActivityInfo activityInfo = ActivityMgr.getActivityInfo(activityId);
        if(activityInfo == null){
            activityInfo = ActivityBussiness.getActivityInfoByActivityId(activityId);
            if(activityInfo == null){
                return null;
            }
            List<Integer> activityIdList = new ArrayList<>();
            activityIdList.add(activityId);
            Map<Integer, List<ActivityConditionInfo>> activityConditionInfoMap = ActivityBussiness.getOpenCrossRankActivityConditionInfoMap(activityIdList);
            initActivityConfig(activityInfo, activityConditionInfoMap.get(activityId));
        }
        return ActivityMgr.getActivityInfo(activityId);
    }
}
